package com.itfreer.forestry.manage;

import com.itfreer.forestry.manage.utils.Sha256;
import com.itfreer.form.api.BaseDao;
import com.itfreer.form.api.BaseServiceImp;
import com.itfreer.power.user.UserAuthenticationService;
import com.itfreer.power.user.dao.UserAuthenticationDao;
import com.itfreer.power.user.dao.UserInfoDao;
import com.itfreer.power.user.entity.UserAuthenticationEntity;
import com.itfreer.power.user.entity.UserInfoEntity;
import com.itfreer.utils.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * Created by 2020-12-23
 *
 * @author yangyang
 */
@Component
public class LocusLerderRequestServiceImpl extends BaseServiceImp<UserInfoEntity> implements LocusLeaderRequestService {

    @Value("${secretKey}")
    private String secretKey;

    @Autowired
    private UserInfoDao dao;

    @Override
    protected BaseDao<UserInfoEntity> getDao() {
        return this.dao;
    }

    @Autowired
    private UserAuthenticationDao userAuthenticationDao;

    @Autowired
    private UserAuthenticationService userAuthenticationService;

    @Override
    public ResponseEntity<Object> addUserInfo(Map<?, ?> entity) {

        String noewtime = (String) entity.get("nowTime");
        if (Sha256.getSHA256(noewtime + secretKey).equals(entity.get("secretKey"))) {

            this.userInfo(entity);
            this.userAuthentication(entity);

            return new ResponseEntity<>("200", "添加成功！");
        } else {
            return new ResponseEntity<>("500", "解密失败！");
        }

    }

    @Override
    public ResponseEntity<Object> updateUserInfo(Map<?, ?> entity) {
        String noewtime = (String) entity.get("nowTime");
        if (Sha256.getSHA256(noewtime + secretKey).equals(entity.get("secretKey"))) {

            this.userInfo(entity);
            this.userAuthentication(entity);

            return new ResponseEntity<>("200", "修改成功！");
        } else {
            return new ResponseEntity<>("500", "解密失败！");
        }

    }
    @Override
    public ResponseEntity<Object> deleteUserInfo(Map<?,?> entity) {
        String noewtime = (String) entity.get("nowTime");
        if (Sha256.getSHA256(noewtime + secretKey).equals(entity.get("secretKey"))) {
            String id = (String) entity.get("id");
            // 删除用户
            this.getDao().delete(id);

            this.getDao().delete(id);
            return new ResponseEntity<>("200", "删除成功！");
        } else {

            return new ResponseEntity<>("200", "解密失败！");
        }
    }

    /**
     * 用户添加修改
     */
    public ResponseEntity<Object> userInfo(Map<?, ?> map) {
        String id = (String) map.get("id");
        String name = (String) map.get("userName");
        String tel = (String) map.get("tel");
        String role = (String) map.get("role");
        if ("forester".equals(role)) {

            role = "38f41f1161fa437280097d1c8b59938d";
        } else if ("linLong".equals(role)) {
            role = "f52e2e6bf3ed41659af39a3a21b6fdfd";
        } else if ("deputyLongLin".equals(role)) {
            role = "f52e2e6bf3ed41659af39a3a21b6fdfd";
        } else if ("from".equals(role)) {
            role = "f52e2e6bf3ed41659af39a3a21b6fdfd";
        }
        String[] roles = role.split(",");



        // 部门
        String departmentId = (String) map.get("zhen");
        String[] departmentIds = null;
        if (departmentId != null) departmentIds = departmentId.split(",");


        String adminDepartment = (String) map.get("cun");
        String[] adminDepartments = null;
        if (adminDepartment != null) adminDepartments = adminDepartment.split(",");


        // 添加用户
        UserInfoEntity userInfoEntity = this.getDao().getEntity(id);
        if (userInfoEntity == null) {
            userInfoEntity = new UserInfoEntity();
            userInfoEntity.setId(id);
            userInfoEntity.setUserName(name);
            userInfoEntity.setTel(tel);
            userInfoEntity.setRoles(roles);
            userInfoEntity.setAdminDepartment(adminDepartments);
            userInfoEntity.setDepartmentId(departmentIds);
            this.getDao().add(userInfoEntity);
        } else {
            if (name != null) userInfoEntity.setUserName(name);
            if (roles != null) userInfoEntity.setRoles(roles);
            if (tel != null) userInfoEntity.setTel(tel);
            if (departmentIds != null) userInfoEntity.setDepartmentId(departmentIds);
            if (adminDepartments != null) userInfoEntity.setAdminDepartment(adminDepartments);
            this.getDao().update(userInfoEntity);
        }
        return new ResponseEntity<>("200", "成功");
    }

    /**
     * 登录授权添加修改
     */
    public ResponseEntity<Object> userAuthentication(Map<?, ?> map) {
        String id = (String) map.get("id");

        // 登录授权
        UserAuthenticationEntity userAuthenticationEntity = userAuthenticationDao.getEntity(id);
        if (userAuthenticationEntity == null) {
            userAuthenticationEntity = new UserAuthenticationEntity();
            userAuthenticationEntity.setId(id);
            userAuthenticationEntity.setAccounts((String) map.get("tel"));
            userAuthenticationEntity.setPassword("123456");
            userAuthenticationEntity.setValid(true);
            userAuthenticationService.add(userAuthenticationEntity);
        } else {
            userAuthenticationEntity.setAccounts((String) map.get("tel"));
            userAuthenticationDao.update(userAuthenticationEntity);
        }
        return new ResponseEntity<>("200", "成功");
    }

}
